all files / src/helpers/ game.helpers.ts

100% Statements 28/28
86.36% Branches 19/22
100% Functions 5/5
100% Lines 27/27
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49    10×                           15×   15×   15× 15× 15×          
import { Game, Phase } from '../game.interfaces';
import { Base64 } from 'js-base64';
import * as _ from 'lodash';
import { defaultState } from '../game.reducer'
 
export function isBomb(pointsValue: number | null): boolean {
    return pointsValue === null;
}
 
export function loadStateFromText(text: string): Game {
    try {
        return JSON.parse(Base64.decode(text));
    } catch (e) { }
    return null;
}
 
export function saveStateToText(state: Game): string {
    return Base64.encode(JSON.stringify(state));
}
 
export function extendStateWithDefaults(game) {
    let loadedState = game ? _.extend({}, defaultState, game) : undefined
 
    if (!loadedState) return loadedState;
 
    loadedState.cards = loadedState.cards || {};
    
    Eif (loadedState.battle) {
        const battle = loadedState.battle;
 
        battle.trickCards = battle.trickCards || [];
        battle.trumpAnnouncements = battle.trumpAnnouncements || [];
        battle.wonCards = battle.wonCards || {};
    }
 
    _.each(loadedState.players, (player) => {
        const battle = loadedState.battle;
 
        loadedState.cards[player.id] = loadedState.cards[player.id] || []
 
        player.battlePoints = player.battlePoints || [];
        Eif (battle) {
            battle.wonCards[player.id] = battle.wonCards[player.id] || [];
        }
    });
 
    return loadedState;
}